import json
import os
import sys

from ATM.conf import settings
from ATM.core import log
from ATM.core import account
from ATM.core import db_handle
from ATM.core import settlement

BARE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
print(BARE_DIR)
file = "%s/ATM/db/accounts/zcl.json" % BARE_DIR
print(file)
with open(file, "r", encoding="utf-8") as f:
    account_data = json.load(f)
    print(account_data)
"""
购物车功能，只能用来进行购物
"""

product_list = [
    ("Apple Iphone", 6000),
    ("Apple Watch", 4600),
    ("Books", 600),
    ("Bike", 750),
    ("cups", 120),
    ("Apple", 50),
    ("banana", 60),
]
shopping_list = []
salary = account_data["balance"]

while True:
    for index, item in enumerate(product_list):
        print(index, item)
    user_choice = input("输入你的系列数字:")
    if user_choice.isdigit():
        user_choice = int(user_choice)
        if user_choice < len(product_list) and user_choice >= 0:
            p_item = product_list[user_choice]
            if p_item[1] < salary:
                shopping_list.append(p_item)
                salary -= p_item[1]
                with open(file, "w+", encoding="utf-8") as f:
                    account_data["balance"] = salary
                    print(account_data)
                    json.dump(account_data, f)
                print("添加%s到你的购物卡，你当前余额为%s" % (p_item, salary))
            else:
                print("你的购物卡余额不足")
        else:
            print("你输入的商品不存在")
    elif user_choice == "q":
        print("====购物列表====")
        for p in shopping_list:
            print(p)
        print("你当前余额为%s" % salary)
        exit()
    else:
        print("错误的选项")
